﻿using Lbb.Drawing;
using Lbb.User;
using Resources;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class members_controls_attachment : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ImgPortrait.ImageUrl = VirtualPathUtility.ToAbsolute(Lbb.User.MemberUser.GetUser().GetAvatar());
        }
    }
    protected void BtnUpLoad_Click(object sender, EventArgs e)
    {
        ProfileCommon profile = Profile;
        var UserID = (Guid)MemberUser.GetUser().ProviderUserKey;

         if (Page.User.Identity.IsAuthenticated)
        {
            if (Request.Files["FUploadImg"] != null && !string.IsNullOrEmpty(Request.Files["FUploadImg"].FileName))
            {
                var imgName = Request.Files["FUploadImg"].FileName;
                string[] imgExtendPatrs = imgName.Split('.');
                string imgExtend = imgExtendPatrs[imgExtendPatrs.Length - 1];
                string imgNewName = UserID + "{0}" + "." + imgExtend;

                var sizeSmall = AvatarSizeHelper.GetAvatarSizes()[AvatarSize.Small];
                var sizeMedium = AvatarSizeHelper.GetAvatarSizes()[AvatarSize.Medium];
                var sizeBit = AvatarSizeHelper.GetAvatarSizes()[AvatarSize.Bit];
                var portraitSmallUrl = string.Format(imgNewName, AvatarSize.Small);
                var portraitMediumUrl = string.Format(imgNewName, AvatarSize.Medium);
                var portraitBitUrl = string.Format(imgNewName, AvatarSize.Bit);


                var path = WebConfig.DefPortraitUrl;//图片虚拟路径
                profile.Portrait = WebConfig.DefPortraitUrl + portraitSmallUrl;
                string physicalPath = Server.MapPath(path);//转换为物理路径

                if (!Directory.Exists(physicalPath))
                {
                    Directory.CreateDirectory(physicalPath);//判断是否存在改路径，如果没有则创建路径
                }
                Request.Files["FUploadImg"].SaveAs(physicalPath + portraitSmallUrl);//保存图片
                Request.Files["FUploadImg"].SaveAs(physicalPath + portraitMediumUrl);//保存图片
                Request.Files["FUploadImg"].SaveAs(physicalPath + portraitBitUrl);//保存图片
                if (sizeSmall[0] > 0)
                {
                    ThumbnailConfig thumconfig = new ThumbnailConfig //生成缩略图
                    {
                        Height = sizeSmall[0],
                        Width = sizeSmall[0],
                        Quality = 80,
                        BackgroundColor = Color.White,
                        ResizeStyle = ResizeStyle.Zoom,
                        MustFit = true
                    };
                    ImageHelper.SaveThumbnail(physicalPath + portraitSmallUrl, thumconfig);//保存缩略图
                }//当Height跟Width等于0的时候，不缩放大小
                if (sizeMedium[0] > 0)
                {
                    ThumbnailConfig thumconfig = new ThumbnailConfig //生成缩略图
                    {
                        Height = sizeMedium[0],
                        Width = sizeMedium[0],
                        Quality = 80,
                        BackgroundColor = Color.White,
                        ResizeStyle = ResizeStyle.Zoom,
                        MustFit = true
                    };
                    ImageHelper.SaveThumbnail(physicalPath + portraitMediumUrl, thumconfig);//保存缩略图
                }//当Height跟Width等于0的时候，不缩放大小
                if (sizeBit[0] > 0)
                {
                    ThumbnailConfig thumconfig = new ThumbnailConfig //生成缩略图
                    {
                        Height = sizeBit[0],
                        Width = sizeBit[0],
                        Quality = 80,
                        BackgroundColor = Color.White,
                        ResizeStyle = ResizeStyle.Zoom,
                        MustFit = true
                    };
                    ImageHelper.SaveThumbnail(physicalPath + portraitBitUrl, thumconfig);//保存缩略图
                }//当Height跟Width等于0的时候，不缩放大小
                profile.Save();
                UpLoadMsg.Text = MemberRes.UpLoadSuccessMsg;
                ImgPortrait.ImageUrl = VirtualPathUtility.ToAbsolute(Lbb.User.MemberUser.GetUser().GetAvatar());
            }
            else
            {
                UpLoadMsg.Text = MemberRes.UpLoadErrorMsg;
            }
           
        }
    }
}